package com.maiji.cloud.controller.shopingmall;

import java.util.List;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.shopingmall.*;
import com.maiji.cloud.response.shopingmall.*;
import com.maiji.cloud.utils.Unlogin;
import org.apache.commons.lang.BooleanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.maiji.cloud.controller.BaseController;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/order")
@Api(tags = { "订单模块" })
public class OrderController extends BaseController {

	/**
	 * 初始化订单数据
	 * 
	 * @param orderInitReqDto
	 * @param token
	 * @return
	 */
	@PostMapping("initOrder")
	@ApiOperation(value = "初始化订单数据")
	public BaseDataResDto<ShopingOrderResData> initOrder(@RequestBody OrderInitReqDto orderInitReqDto,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingOrder/initOrder", orderInitReqDto,
				BaseDataResDto.class);
	}

	/**
	 * 刷新订单数据
	 * 
	 * @param orderRefreshReqDto
	 * @param token
	 * @return
	 */
	@PostMapping("refreshOrder")
	@ApiOperation(value = "刷新订单数据")
	public BaseDataResDto<ShopingOrderResData> refreshOrder(@RequestBody OrderRefreshReqDto orderRefreshReqDto,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingOrder/refreshOrder", orderRefreshReqDto,
				BaseDataResDto.class);
	}

	/**
	 * 提交订单
	 * 
	 * @param orderSubmitReqDto
	 * @param token
	 * @return
	 */
	@PostMapping("orderSubmit")
	@ApiOperation(value = "提交订单")
	public BaseDataResDto<ShopingOrderResData> orderSubmit(@RequestBody OrderSubmitReqDto orderSubmitReqDto,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingOrder/orderSubmit", orderSubmitReqDto,
				BaseDataResDto.class);
	}

	/**
	 * 获取订单详情
	 * 
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("findOrderById")
	@ApiOperation(value = "获取订单详情")
	public BaseDataResDto<ShopingOrderResData> findOrderById(@RequestBody BaseDataReqDto<String> baseDataReqDto,
			@RequestHeader("maijiToken") String token) {
		return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/shopingOrder/findOrderById", baseDataReqDto, BaseDataResDto.class);
	}

	/**
	 * 获取退款/售后订单详情
	 * 
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("findRefundOrderById")
	@ApiOperation(value = "获取退款/售后订单详情")
	public BaseDataResDto<ShopingOrderResData> findRefundOrderById(@RequestBody BaseDataReqDto<String> baseDataReqDto,
			@RequestHeader("maijiToken") String token) {
		return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/shopingOrder/findOrderById", baseDataReqDto, BaseDataResDto.class);
	}

	/**
	 * 取消订单
	 * 
	 * @param orderCancelReqDto
	 * @return
	 */
	@PostMapping("cancelOrderById")
	@ApiOperation(value = "取消订单")
	public BaseDataResDto<ShopingOrderResData> cancelOrderById(@RequestBody OrderCancelReqDto orderCancelReqDto,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingOrder/cancelOrderById", orderCancelReqDto,
				BaseDataResDto.class);
	}

	/**
	 * 删除 / 批量删除 订单
	 * 
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("delOrdersByIds")
	@ApiOperation(value = "删除 / 批量删除 订单")
	public BaseResDto delOrdersByIds(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto,
			@RequestHeader("maijiToken") String token) {
		return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/shopingOrder/delOrdersByIds", baseDataReqDto,
				BaseResDto.class);
	}

	/**
	 * 根据订单状态获取用户订单列表
	 * @param baseDataReqDto
	 * @param token
	 * @return
	 */
	@PostMapping("findAllOrdersByUserId")
	@ApiOperation(value = "根据订单状态获取用户订单列表")
	public BaseMetaResDto<List<ShopingOrderResData>> findAllOrdersByUserId(@RequestBody BaseDataReqDto<Integer> baseDataReqDto,
                                                                           @RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingOrder/findAllOrdersByUserId", baseDataReqDto, BaseMetaResDto.class);
	}

	/**
	 * 根据商品名关键词搜索用户订单列表
	 * @param baseDataReqDto
	 * @param token
	 * @return
	 */
	@PostMapping("findOrdersByUserId")
	@ApiOperation(value = "根据商品名关键词搜索用户订单列表")
	public BaseMetaResDto<List<ShopingOrderResData>> findOrdersByUserId(@RequestBody BaseDataReqDto<String> baseDataReqDto,
                                                                           @RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingOrder/findOrdersByUserId", baseDataReqDto, BaseMetaResDto.class);
	}

	/**
	 * 获取用户退款/售后订单列表
	 * 
	 * @param baseDataReqDto
	 * @param token
	 * @return
	 */
	@PostMapping("findAllRefundOrders")
	@ApiOperation(value = "获取用户退款/售后订单列表")
	public BaseMetaResDto<List<ShopingOrderResData>> findAllRefundOrders(@RequestBody BaseDataReqDto<Integer> baseDataReqDto,
                                                                         @RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingOrder/findAllRefundOrders", baseDataReqDto, BaseMetaResDto.class);
	}

	/**
	 * 提醒订单发货
	 * 
	 * @param baseReqMetaDto
	 * @param token
	 * @return
	 */
	@PostMapping("remindSendByOrderId")
	@ApiOperation(value = "提醒订单发货")
	public BaseResDto remindSendByOrderId(@RequestBody BaseDataReqDto<String> baseReqMetaDto,
			@RequestHeader("maijiToken") String token) {
		return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/shopingOrder/remindSendByOrderId", baseReqMetaDto,
				BaseResDto.class);
	}

	/**
	 * 获取各个状态的订单数量
	 * @param token
	 * @return
	 */
	@PostMapping("findAllShopingOrderStatusCount")
	@ApiOperation(value = "获取各个状态的订单数量")
	public BaseDataResDto<List<ShopingOrderStatusCountResData>> findAllShopingOrderStatusCount(@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingOrder/findAllShopingOrderStatusCount", null, BaseDataResDto.class);
	}

	/**
	 * 商品加入购物车
	 * 
	 * @param addToShopingCartReqDto
	 * @param token
	 * @return
	 */
	@PostMapping("addToShopingCart")
	@ApiOperation(value = "商品加入购物车")
	public BaseResDto addToShopingCart(@RequestBody AddToShopingCartReqDto addToShopingCartReqDto,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingCart/addToShopingCart",
				addToShopingCartReqDto, BaseResDto.class);
	}

	/**
	 * 商品批量加入购物车
	 * 
	 * @param baseDataReqDto
	 * @param token
	 * @return
	 */
	@PostMapping("addBatchToShopingCart")
	@ApiOperation(value = "商品批量加入购物车")
	public BaseResDto addBatchToShopingCart(@RequestBody BaseDataReqDto<List<String>> baseDataReqDto,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingCart/addBatchToShopingCart",
				baseDataReqDto, BaseResDto.class);
	}

	/**
	 * 获取购物车商品列表
	 * 
	 * @param token
	 * @return
	 */
	@PostMapping("findAllShopingCart")
	@ApiOperation(value = "获取购物车商品列表")
	public BaseMetaResDto<List<ShopingCartResData>> FindAllShopingCart(@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingCart/findAllShopingCart", null,
				BaseMetaResDto.class);
	}

	/**
	 * 获取用户购物车数量
	 * @param token
	 * @return
	 */
	@PostMapping("findUserShopingCartCount")
	@ApiOperation(value = "获取用户购物车数量")
	public BaseDataResDto<Integer> findUserShopingCartCount(@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingCart/findUserShopingCartCount", null,
                BaseDataResDto.class);
	}

	/**
	 * 选择购物车商品（全选）
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("checkShopingCart")
	@ApiOperation(value = "选择购物车商品（全选）")
	public BaseDataResDto<ShopingCartChosenResData> checkShopingCart(@RequestBody BaseDataReqDto<ShopingCartCheckReqData> baseDataReqDto,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingCart/checkShopingCart", baseDataReqDto,
				BaseDataResDto.class);
	}

	/**
	 * 增加或减少购物车商品数量
	 * 
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("updateShopingCartQuantity")
	@ApiOperation(value = "增加或减少购物车商品数量")
	public BaseResDto updateShopingCartQuantity(@RequestBody BaseDataReqDto<ShopingCartQuantityUpdateReqData> baseDataReqDto,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingCart/updateShopingCartQuantity",
				baseDataReqDto, BaseResDto.class);
	}

	/**
	 * 批量删除购物车商品
	 * 
	 * @param shopingCartDelReqDto
	 * @return
	 */
	@PostMapping("delShopingCart")
	@ApiOperation(value = "批量删除购物车商品")
	public BaseResDto delShopingCart(@RequestBody ShopingCartDelReqDto shopingCartDelReqDto,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingCart/delShopingCart", shopingCartDelReqDto,
				BaseResDto.class);
	}

	/**
	 * 添加新/修改收货地址
	 * 
	 * @param shopingAddressAddReqDto
	 * @param token
	 * @return
	 */
	@PostMapping("addOrUpdateShopingAddress")
	@ApiOperation(value = "添加新/修改收货地址")
	public BaseDataResDto<String> addOrUpdateShopingAddress(@RequestBody ShopingAddressAddReqDto shopingAddressAddReqDto,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shippingAddress/addOrUpdateShopingAddress",
				shopingAddressAddReqDto, BaseDataResDto.class);
	}

	/**
	 * 获取收货地址列表
	 * 
	 * @param token
	 * @return
	 */
	@PostMapping("findAllShopingAddress")
	@ApiOperation(value = "获取收货地址列表")
	public BaseMetaResDto<List<ShopingAddressResData>> findAllShopingAddress(
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shippingAddress/findAllShopingAddress", null,
				BaseMetaResDto.class);
	}

	/**
	 * 删除收货地址
	 * 
	 * @param findByIdReqDto
	 * @return
	 */
	@PostMapping("delShopingAddress")
	@ApiOperation(value = "删除收货地址")
	public BaseResDto delShopingAddress(@RequestBody FindByIdReqDto findByIdReqDto,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shippingAddress/delShopingAddress",
				findByIdReqDto, BaseResDto.class);
	}

	/**
	 * 根据订单id获取运单编号集合
	 * 
	 * @param param
	 * @return
	 */
	@ApiOperation(value = "订单id获得运单号集合")
	@PostMapping("getTrackingNumberByOrderId")
	public BaseDataResDto<List<String>> getTrackingNumberByOrderId(@RequestBody BaseDataReqDto<String> param,
			@RequestHeader("maijiToken") String token) {

		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingOrder/getTrackingNumberByOrderId", param,
				BaseDataResDto.class);
	}

	/**
	 * 根据运单号获取物流轨迹
	 * 
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@PostMapping("getLogisticsInfoByExpressNo")
	@ApiOperation(value = "根据运单号获取物流轨迹")
	public BaseDataResDto<LogisticsInfoResData> getLogisticsInfoByExpressNo(@RequestBody BaseDataReqDto<String> param,
			@RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token, REST_URL_SHOPINGMALL + "/shopingOrder/getLogisticsInfoByExpressNo", param,
				BaseDataResDto.class);
	}

	/**
	 * 根据订单ID判断订单是否可以助力返现
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("ifCanShareOrder")
	@ApiOperation(value = "根据订单ID判断订单是否可以助力返现")
	public BaseDataResDto<ShareOrderLogResData> ifCanShareOrder(@RequestBody BaseDataReqDto<String> baseDataReqDto,
                                                                @RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token,REST_URL_SHOPINGMALL + "/shopingOrder/ifCanShareOrder", baseDataReqDto, BaseDataResDto.class);
	}

	/**
	 * 订单返现用户助力
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("shareOrderHelp")
	@ApiOperation(value = "订单返现用户助力（根据UUID，即返现活动ID）")
	public BaseDataResDto<Double> shareOrderHelp(@RequestBody BaseDataReqDto<String> baseDataReqDto,
                                                 @RequestHeader("maijiToken") String token) {
		return postForObjectWithToken(token,REST_URL_SHOPINGMALL + "/shopingOrder/shareOrderHelp", baseDataReqDto, BaseDataResDto.class);
	}

	/**
	 * 查看订单返现分享助力详情（根据订单ID）
	 * @param baseDataReqDto
	 * @return
	 */
    @Unlogin
	@PostMapping("findShareOrderHelpById")
	@ApiOperation(value = "查看订单返现分享助力详情（根据订单ID）")
	public BaseDataResDto<ShareOrderLogResData> findShareOrderHelpById(@RequestBody BaseDataReqDto<String> baseDataReqDto,
                                                                       @RequestHeader(name = "maijiToken", required = false) String token) {
		return postForObjectWithToken(token,REST_URL_SHOPINGMALL + "/shopingOrder/findShareOrderHelpById", baseDataReqDto, BaseDataResDto.class);
	}

	/**
	 * 订单确认收货
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("orderConfirmReceived")
	@ApiOperation(value = "订单确认收货")
	public BaseResDto orderConfirmReceived(@RequestBody BaseDataReqDto<String> baseDataReqDto, @RequestHeader("maijiToken") String token) {
		return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/shopingOrder/orderConfirmReceived", baseDataReqDto, BaseResDto.class);
	}

	/**
	 * 查看订单返现活动列表
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("findAllShareOrderHelpLogs")
	@ApiOperation(value = "查看订单返现活动列表：0未结算，1已结算，2全部")
	public BaseMetaResDto<List<ShareOrderLogResData>> findAllShareOrderHelpLogs(@RequestBody BaseDataReqDto<Integer> baseDataReqDto,
                                                                            @RequestHeader("maijiToken") String token) {
        Integer settlement = baseDataReqDto.getData();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        ShareOrderLogFindAllData shareOrderLogFindAllData = new ShareOrderLogFindAllData();
        if (settlement != 2) shareOrderLogFindAllData.setSettlement(BooleanUtils.toBoolean(settlement));
        return postForObjectWithToken(token,REST_URL_SHOPINGMALL + "/shopingOrder/findAllShareOrderHelpLogs",
                new BaseDataReqDto().setData(shareOrderLogFindAllData).setMetaData(metaData), BaseMetaResDto.class);
	}

	/**
	 * 用户提前结算订单返现分享助力
	 * @param baseDataReqDto
	 * @return
	 */
	@PostMapping("statisticShareOrderHelp")
	@ApiOperation(value = "用户提前结算订单返现分享助力")
	public BaseDataResDto<Double> statisticShareOrderHelp(@RequestBody BaseDataReqDto<String> baseDataReqDto, @RequestHeader("maijiToken") String token) {
        return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/shopingOrder/statisticShareOrderHelp",  baseDataReqDto, BaseDataResDto.class);
	}
	
	
	 /**
     * 上传运单号
     * @param param
     * @return
     */
    @PostMapping("uploudRefundNum")
    @ApiOperation(value = "上传运单号")
    public BaseResDto uploudRefundNum(@RequestBody BaseDataReqDto<UploudRefundNumReqData> param, @RequestHeader("maijiToken") String token){
		
    	return restTemplate.postForObject(REST_URL_SHOPINGMALL + "/shopingOrder/uploudRefundNum",  param, BaseResDto.class);
    }

}
